package com.iwomy.secureplat.platform.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.iwomy.secureplat.common.core.util.R;
import com.iwomy.secureplat.common.log.annotation.SysLog;
import com.iwomy.secureplat.common.security.util.SecurityUtils;
import com.iwomy.secureplat.platform.dto.AlarmFaultLoggingDTO;
import com.iwomy.secureplat.platform.entity.AlarmFaultLoggingEntity;
import com.iwomy.secureplat.platform.entity.AlarmLoggingEntity;
import com.iwomy.secureplat.platform.entity.BasicDeviceEntity;
import com.iwomy.secureplat.platform.service.AlarmFaultLoggingService;
import com.iwomy.secureplat.platform.service.AlarmLoggingService;
import com.iwomy.secureplat.platform.service.BasicDeviceService;
import org.springframework.security.access.prepost.PreAuthorize;
import com.iwomy.secureplat.common.excel.annotation.ResponseExcel;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.security.Security;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

/**
 * 故障报警记录表
 *
 * @author pig
 * @date 2023-12-24 15:06:44
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/alarmFaultLogging")
@Tag(description = "alarmFaultLogging", name = "故障报警记录表管理")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class AlarmFaultLoggingController {

	private final AlarmFaultLoggingService alarmFaultLoggingService;

	private final BasicDeviceService basicDeviceService;

	private final AlarmLoggingService alarmLoggingService;

	/**
	 * 分页查询
	 *
	 * @param page                 分页对象
	 * @param alarmFaultLoggingDTO 故障报警记录表
	 * @return
	 */
	@Operation(summary = "分页查询", description = "分页查询")
	@GetMapping("/page")
	@PreAuthorize("@pms.hasPermission('platform_alarmFaultLogging_view')")
	public R getAlarmFaultLoggingPage(@ParameterObject Page page, @ParameterObject AlarmFaultLoggingDTO alarmFaultLoggingDTO) {
		LambdaQueryWrapper<AlarmFaultLoggingEntity> wrapper = Wrappers.lambdaQuery();
		return R.ok(alarmFaultLoggingService.getAlarmFaultLoggingPage(page, alarmFaultLoggingDTO));
	}


	/**
	 * 通过id查询故障报警记录表
	 *
	 * @param id id
	 * @return R
	 */
	@Operation(summary = "通过id查询", description = "通过id查询")
	@GetMapping("/{id}")
	@PreAuthorize("@pms.hasPermission('platform_alarmFaultLogging_view')")
	public R getById(@PathVariable("id") Long id) {
		return R.ok(alarmFaultLoggingService.getById(id));
	}

	/**
	 * 新增故障报警记录表
	 *
	 * @param alarmFaultLogging 故障报警记录表
	 * @return R
	 */
	@Operation(summary = "新增故障报警记录表", description = "新增故障报警记录表")
	@SysLog("新增故障报警记录表")
	@PostMapping
	@PreAuthorize("@pms.hasPermission('platform_alarmFaultLogging_add')")
	public R save(@RequestBody AlarmFaultLoggingEntity alarmFaultLogging) {
		alarmFaultLogging.setDeptId(SecurityUtils.getUser().getDeptId());
		return R.ok(alarmFaultLoggingService.save(alarmFaultLogging));
	}

	/**
	 * 修改故障报警记录表
	 *
	 * @param alarmFaultLoggingDTO 故障报警记录表
	 * @return R
	 */
	@Operation(summary = "修改故障报警记录表", description = "修改故障报警记录表")
	@SysLog("修改故障报警记录表")
	@PutMapping
	@PreAuthorize("@pms.hasPermission('platform_alarmFaultLogging_edit')")
	public R updateById(@RequestBody AlarmFaultLoggingDTO alarmFaultLoggingDTO) {

		AlarmFaultLoggingEntity alarmFaultLoggingEntity = BeanUtil.copyProperties(alarmFaultLoggingDTO, AlarmFaultLoggingEntity.class);
		if (alarmFaultLoggingDTO.getState() == 10) {
			alarmFaultLoggingEntity.setProcessingTime(LocalDateTime.now());
			alarmFaultLoggingService.updateById(alarmFaultLoggingEntity);
		} else {
			return R.ok();
		}
		AlarmFaultLoggingEntity byId = alarmFaultLoggingService.getById(alarmFaultLoggingEntity.getId());
		AlarmLoggingEntity alarmLoggingEntity = alarmLoggingService.oneByImeiDesc(byId.getImei());
		AlarmFaultLoggingEntity alarmFaultLogging = alarmFaultLoggingService.oneByImeiDesc(byId.getImei());
		BasicDeviceEntity basicDeviceEntity = new BasicDeviceEntity();
		basicDeviceEntity.setId(byId.getDeviceId());
		if (ObjectUtil.isNotNull(alarmLoggingEntity) && ObjectUtil.isNotNull(alarmFaultLogging)) {
			basicDeviceEntity.setDeviceState(alarmFaultLogging.getCreateTime()
					.compareTo(alarmLoggingEntity.getCreateTime()) > 0 ? 30 : 40);
		} else if (ObjectUtil.isNotNull(alarmLoggingEntity)) {
			basicDeviceEntity.setDeviceState(40);
		} else if (ObjectUtil.isNotNull(alarmFaultLogging)) {
			basicDeviceEntity.setDeviceState(30);
		} else {
			basicDeviceEntity.setDeviceState(20);
		}

		//重置设备状态


		basicDeviceService.updateById(basicDeviceEntity);

		return R.ok();
	}

	/**
	 * 通过id删除故障报警记录表
	 *
	 * @param ids id列表
	 * @return R
	 */
	@Operation(summary = "通过id删除故障报警记录表", description = "通过id删除故障报警记录表")
	@SysLog("通过id删除故障报警记录表")
	@DeleteMapping
	@PreAuthorize("@pms.hasPermission('platform_alarmFaultLogging_del')")
	public R removeById(@RequestBody Long[] ids) {
		return R.ok(alarmFaultLoggingService.removeBatchByIds(CollUtil.toList(ids)));
	}


	/**
	 * 导出excel 表格
	 *
	 * @param alarmFaultLogging 查询条件
	 * @param ids               导出指定ID
	 * @return excel 文件流
	 */
	@ResponseExcel
	@GetMapping("/export")
	@PreAuthorize("@pms.hasPermission('platform_alarmFaultLogging_export')")
	public List<AlarmFaultLoggingEntity> export(AlarmFaultLoggingEntity alarmFaultLogging, Long[] ids) {
		return alarmFaultLoggingService.list(Wrappers.lambdaQuery(alarmFaultLogging).in(ArrayUtil.isNotEmpty(ids), AlarmFaultLoggingEntity::getId, ids));
	}

	/**
	 * @Return R
	 */
	@Operation(summary = "近3天的报警统计", description = "近3天的报警统计")
	@GetMapping("/threeDaysFaultCount")
	public R threeDaysFaultCount() {
		return alarmFaultLoggingService.threeDaysFaultCount();
	}

}